﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Citi.Scheduler.Domain;
using Citi.Scheduler.WebUi.Models;

namespace Citi.Scheduler.WebUi.Controllers
{
    public class RoutineErrorHistoryController : ControllerBase<RoutineErrorHistoryModel, RoutineErrorHistory>
    {
        public ActionResult Index(int routineErrorId)
        {
            var models = new RoutineErrorHistoryModel().CreateModel(new RoutineErrorHistory().SelectByRoutineErrorId(routineErrorId));
            return PartialView(models);
        }

        public ActionResult Form(int routineErrorId)
        {
            return View(new RoutineErrorHistoryModel { RoutineErrorId = routineErrorId });
        }

        [OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
        public ActionResult ActionForm(int? id, int routineErrorId)
        {
            FillViewBags();
            //var routineError = new RoutineError().SelectById<RoutineError>(routineErrorId);
            var history = new RoutineErrorHistoryModel { RoutineErrorId = routineErrorId, Date = DateTime.Today };
            return PartialView(history);
        }

        [HttpPost]
        public ActionResult ActionForm(RoutineErrorHistoryModel model)
        {
            return ActionBase(model, true);
        }

        public void FillViewBags(int? userId = null)
        {
            var users = new User().SelectAllActive<User>().OrderBy(m => m.Name);
            ViewBag.Users = users.ToSelectList(x => x.Name, x => x.Id.ToString(), "Select a user",
                userId.HasValue ? userId.Value.ToString() : string.Empty);

            ViewBag.Status = new RoutineErrorStatus().ToSelectList(true, true);
        }
    }
}
